package cn.kgc.musicserver.controller;


import cn.hutool.core.util.ObjUtil;
import cn.kgc.musicserver.domain.dto.Result;
import cn.kgc.musicserver.domain.dto.ResultUtil;
import cn.kgc.musicserver.domain.entity.Admin;
import cn.kgc.musicserver.service.IAdminService;
import cn.kgc.musicserver.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author kgc
 * @since 2025-06-18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminController {
    private final IAdminService adminService;

    /**
     * 登录
     * @param admin
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        //封装查询条件
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("name", admin.getName());
        wrapper.eq("password", admin.getPassword());
        //调用查询
        Admin loginAdmin = adminService.getOne(wrapper);
        //判断是否查询到用户
        if(ObjUtil.isNotNull(loginAdmin)) {
            String token = JwtUtil.createJWT(loginAdmin.getName());
            return ResultUtil.returnSuccess("登录成功", token);
        }
        return ResultUtil.returnFail("用户名或密码错误");
    }


    // ================= 新增接口：管理员信息管理（/gadmin/xxx）=================

    /**
     * 获取管理员详情
     */
    @GetMapping("/gadmin/detail")
    public Result getAdminDetail(@RequestParam Long id) {
        Admin admin = adminService.getById(id);
        if (ObjUtil.isNotNull(admin)) {
            return ResultUtil.returnSuccess("获取成功", admin);
        }
        return ResultUtil.returnFail("未找到该管理员");
    }

    /**
     * 分页获取管理员列表
     */
    @RequestMapping("gadmin/page")
    public Result getSingerPage(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                @RequestParam(value = "size", defaultValue = "5") int size,
                                @RequestParam(value = "search", required = false) String search) {
        IPage<Admin> page = new Page<>(pageNo, size);
        page = adminService.page(page, search != null ? new QueryWrapper<Admin>().like("name", search) : null);
        return ResultUtil.returnDataSuccess(page);
    }
    /**
     * 添加管理员
     */
    @PostMapping("/gadmin/add")
    public Result addAdmin(@RequestBody Admin admin) {
        boolean success = adminService.save(admin);
        if (success) {
            return ResultUtil.returnSuccess("添加成功");
        }
        return ResultUtil.returnFail("添加失败");
    }

    /**
     * 更新管理员
     */
    @PostMapping("/gadmin/update")
    public Result updateAdmin(@RequestBody Admin admin) {
        boolean success = adminService.updateById(admin);
        if (success) {
            return ResultUtil.returnSuccess("更新成功");
        }
        return ResultUtil.returnFail("更新失败");
    }

    /**
     * 删除管理员
     */
    @GetMapping("/gadmin/delete")
    public Result deleteAdmin(@RequestParam Long id) {
        boolean success = adminService.removeById(id);
        if (success) {
            return ResultUtil.returnSuccess("删除成功");
        }
        return ResultUtil.returnFail("删除失败");
    }
}
